﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using BlueFlyPhone.Model;


// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641

namespace BlueFlyPhone
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {

        VarioModel _model;
        //Bluefly.VarioService _service;

        Bluefly.IVarioService _service;

        Bluefly.PositionService _position;


        Windows.System.Display.DisplayRequest KeepScreenOnRequest;


        public MainPage()
        {


            IGCFile file = new IGCFile();

            //file.Flight.Add(new IGCFile.BRecord(DateTime.Now, 101325, -42.5337, 172.6505, 20));

            //file.Flight.Add(new IGCFile.BRecord(DateTime.Now, 101125));

            //file.WriteFile("");

             

            this.InitializeComponent();
            
            //_service = new Bluefly.VarioSimulationService();

            _service = new Bluefly.VarioService();
            _position = new Bluefly.PositionService();
           

            _model = new VarioModel(_service);
            
            this.DataContext = _model;
            this.NavigationCacheMode = NavigationCacheMode.Required;

            KeepScreenOnRequest = new Windows.System.Display.DisplayRequest();

            KeepScreenOnRequest.RequestActive();

            //this.txtqnh.Text = PressureUnitConverter.Current.ConvertToDisplayUnits(_service.SetAltitude(0),0).ToString();
            //this.txtAlt.Text = "?";

        }

        void _service_ConnectionStateUpdated(object sender, Bluefly.ConnectionStateEventArgs e)
        {


           
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // TODO: Prepare page for display here.

            // TODO: If your application contains multiple pages, ensure that you are
            // handling the hardware Back button by registering for the
            // Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
            // If you are using the NavigationHelper provided by some templates,
            // this event is handled for you.
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

            if (_service == null)
                return;

            if (_service.IsConnected)
            {
                _service.Stop();

                //btnConnect.Content = "Connect";
            }
            else
            {
                _service.Run();
                //btnConnect.Content = "Stop";
            }
        }

        private  void btnNavigateBlueToothSettings_Click(object sender, RoutedEventArgs e)
        {
            var ignored = Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings-bluetooth:///"));

        } 

        private void btnSetQNH_Click(object sender, RoutedEventArgs e)
        {
            setQNH();
        }

        private void btnSetAltitude_Click(object sender, RoutedEventArgs e)
        {
            SetAltitude();
        }


        void setQNH()
        { 
        
            double dvalue;

            if (double.TryParse(this.txtqnh.Text, out dvalue))
            {
                this.txtAlt.Text = AltitudeUnitConverter.Current.ConvertToDisplayUnits(
                    _service.SetQNH(
                    PressureUnitConverter.Current.ConvertToCalculationUnits(dvalue) + 25), 0);
            }
            else
            {

            }
        
        }

        void SetAltitude()
        { 
        
            double dvalue;

            if (double.TryParse(this.txtAlt.Text, out dvalue))
            {
                this.txtqnh.Text = PressureUnitConverter.Current.ConvertToDisplayUnits(
                    _service.SetAltitude(AltitudeUnitConverter.Current.ConvertToCalculationUnits(dvalue)), 0);
            }
            else
            {

            }
        }



        
        void IncrementQNH(double amount)
        {
             double dvalue;

            if (double.TryParse(this.txtqnh.Text, out dvalue))
            {
                this.txtqnh.Text = (dvalue + amount).ToString();
            }

            setQNH();
        }

        private void Button_IncreaseQNH_Click(object sender, RoutedEventArgs e)
        {
            IncrementQNH(1);
        }



        private void Button_DecreaseQNH_Click(object sender, RoutedEventArgs e)
        {

            IncrementQNH(-1);

        }
    }
}
